package zdh.jfinabbsapp.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.utils.KJLoger;

import java.util.ArrayList;
import java.util.List;

import zdh.jfinabbsapp.api.dao.GeneralTopic;
import zdh.jfinabbsapp.api.dao.Reply;
import zdh.jfinabbsapp.api.dao.Topic;

/**
 * 作者：zdh1000
 * 创建时间： 2015/6/30 21:09
 * 邮箱：wdzdh1000@163.com
 */
public class TopicAPI {
    //默认一页显示主题数
    private static int pageSize = 10;
    //栏目内主题链接，需要参数p,tab,size
    private static String TOPICLIST = API.ROOT+"/index/?&p=$page&tab=$tab&size=$size";
    //主题详情的链接，需要参数id
    private static String TOPIC = API.ROOT+"/topic/$id";

    //获取栏目列表api的链接
    public static String getTopicListUrl(String tab, int page) {
        String url = TOPICLIST.replace("$page", String.valueOf(page));
        url = url.replace("$tab", tab);
        url = url.replace("$size", String.valueOf(pageSize));
        KJLoger.debug(TopicAPI.class.getName() + " : " + url);
        return url;
    }

    //根据下载的json数据，提取栏目里面的主题列表
    public static List<GeneralTopic> getTopicListByJson(String json) {
        List<GeneralTopic> generalTopicList = new ArrayList<>();
        try {
            KJLoger.debug(TopicAPI.class.getName() + " : " + json);
            JSONObject jsonObject = new JSONObject(json);
            JSONArray array = jsonObject.getJSONObject("detail").getJSONArray("list");
            GeneralTopic topic;
            JSONObject temp;
            //依次添加topic
            for (int i = 0; i < array.length(); i++) {
                temp = array.getJSONObject(i);
                topic = new GeneralTopic();
                topic.setInTime(temp.getString("in_time"));
                topic.setOriginalUrl(temp.getString("original_url"));
                topic.setNickName(temp.getString("nickname"));
                topic.setReplyCount(temp.getInt("reply_count"));
                topic.setTab(temp.getString("tab"));
                topic.setGood(temp.getInt("good"));
                topic.setSectionName(temp.getString("sectionName"));
                topic.setAvatar(temp.getString("avatar"));
                topic.setId(temp.getString("id"));
                topic.setContent(temp.getString("content"));
                topic.setTitle(temp.getString("title"));
                topic.setReposted(temp.getInt("reposted"));
                topic.setSid(temp.getInt("s_id"));
                topic.setAuthorId(temp.getString("author_id"));
                topic.setView(temp.getInt("view"));
                topic.setModifyTime(temp.getString("modify_time"));
                topic.setShowStatus(temp.getInt("show_status"));
                topic.setTop(temp.getInt("top"));
                generalTopicList.add(topic);
            }
        } catch (JSONException e) {
            KJLoger.debug(TopicAPI.class.getName() + " : " + e.getMessage());
        }
        return generalTopicList;
    }

    //获取主题详情的api链接
    public static String getTopicUrl(String id) {
        String url = TOPIC.replace("$id", id);
        KJLoger.debug(TopicAPI.class.getName() + " url : " + url);
        return url;
    }

    //根据下载的json数据，提取主题详情
    public static Topic getTopicByJson(String json) {
        KJLoger.debug(TopicAPI.class.getName() + " json : " + json);
        Topic topic = new Topic();
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONObject detailObject = jsonObject.getJSONObject("detail");
            JSONObject topicObject = detailObject.getJSONObject("topic");
            JSONArray replyObject = detailObject.getJSONArray("replies");

            topic.setInTime(topicObject.getString("in_time"));
            topic.setOriginalUrl(topicObject.getString("original_url"));
            topic.setNickName(topicObject.getString("nickname"));
            topic.setReplyCount(topicObject.getInt("reply_count"));
            topic.setTab(topicObject.getString("tab"));
            topic.setGood(topicObject.getInt("good"));
            topic.setSectionName(topicObject.getString("sectionName"));
            topic.setAvatar(topicObject.getString("avatar"));
            topic.setId(topicObject.getString("id"));
            topic.setContent(topicObject.getString("content"));
            topic.setTitle(topicObject.getString("title"));
            topic.setReposted(topicObject.getInt("reposted"));
            topic.setSid(topicObject.getInt("s_id"));
            topic.setAuthorId(topicObject.getString("author_id"));
            topic.setView(topicObject.getInt("view"));
            topic.setModifyTime(topicObject.getString("modify_time"));
            topic.setShowStatus(topicObject.getInt("show_status"));
            topic.setTop(topicObject.getInt("top"));
            topic.setScore(topicObject.getInt("score"));
            topic.setSignature(topicObject.getString("signature"));

            Reply reply;
            JSONObject temp;
            //依次添加replies
            for (int i = 0; i < replyObject.length(); i++) {
                temp = replyObject.getJSONObject(i);
                reply = new Reply();
                reply.setQuoteContent(temp.getString("quote_content"));
                reply.setContent(temp.getString("content"));
                reply.setId(temp.getString("id"));
                reply.setInTime(temp.getString("in_time"));
                reply.setNickname(temp.getString("nickname"));
                reply.setQuote(temp.getInt("quote"));
                reply.setAuthorId(temp.getString("author_id"));
                reply.setTid(temp.getString("tid"));
                reply.setAvatar(temp.getString("avatar"));
                reply.setQuoteAuthorNickname(temp.getString("quote_author_nickname"));
                topic.addReply(reply);
            }
        } catch (JSONException e) {
            KJLoger.debug(TopicAPI.class.getName() + " exception: " + e.getMessage());
        }
        return topic;
    }

}
